package cn.controller;

import cn.entity.User;
import cn.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/usercontroller")
public class UserController {

    @Autowired
    @Qualifier("userService")
    public UserService userService;

    @RequestMapping("/begin")
    public ModelAndView begin(){
        return  new ModelAndView("list");
    }

    @RequestMapping("/getAllTotle")
    public ModelAndView getAllUserTotle(User user){
        ModelAndView mv = new ModelAndView();
        int num =  userService.getUserTotle();
        user.setId(num);
        mv.addObject("user",user);
        mv.setViewName("list");
        return mv;
    }
    @RequestMapping("/getAllUser")
    public Map<String ,Object> getUserList(int curPage,int perTotle,User user){
        Map<String ,Object> map = new HashMap<>();
        map.put("curPage",curPage);
        map.put("perTotle",perTotle);
        map.put("user",user);
        Page page = userService.getUserList(map);
        map.put("page",page);
        map.put("totle",page.getPages());
        return map;

    }
    @RequestMapping("/addUser")
    public ModelAndView addUser(User user){
        ModelAndView mv = new ModelAndView();
        boolean b = userService.addUser(user);

        mv.setViewName("redirect:/getAllUser.do");
        return mv;
    }


}
